<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $ispList app\models\Isp[]
 * @var $pagination \yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '运营商列表';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<table class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">
                <label class="pos-rel">
                    <input type="checkbox" class="ace" />
                    <span class="lbl"></span>
                </label>
            </th>
            <th>名称</th>
            <th>接口</th>
            <th>参数</th>
            <th>操作</th>
        </tr>
    </thead>

    <tbody>
        <?php foreach ($ispList as $isp) {?>
            <tr id="data_<?php echo $isp->id;?>">
                <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $isp->id;?>" /><span class="lbl"><?php echo $isp->id;?></span></label></td>
                <td><?php echo Html::encode($isp->name);?></td>
                <td><?php echo Html::encode($isp->api->name);?></td>
                <td><?php $param = json_decode($isp->api->param, true);
                    $data = json_decode($isp->data, true);
                    echo '<dl class="dl-horizontal">';
                    foreach ($param as $p) {
                        echo '<dt>', Html::encode($p), '</dt><dd>' . Html::encode(isset($data[$p]) ? $data[$p] : ''), '</dd>';
                    }
                    echo '</dl>';?>
                </td>
                <td><?php echo ManagerTableOp::widget(['items' => [
                        ['icon' => 'fa fa-info-circle', 'href' => Url::to(['/admin/card/isp-view', 'id' => $isp->id]), 'btn_class' => 'btn btn-xs btn-default', 'tip' => '详情'],
                        ['icon' => 'fa fa-pencil', 'href' => Url::to(['/admin/card/isp-edit', 'id' => $isp->id]), 'btn_class' => 'btn btn-xs btn-success', 'tip' => '修改', 'color' => 'green'],
                        ['icon' => 'fa fa-trash', 'onclick' => 'deleteIsp(' . $isp->id . ')', 'btn_class' => 'btn btn-xs btn-danger', 'tip' => '删除'],
                        ['icon' => 'fa fa-pie-chart', 'href' => Url::to(['/admin/card/isp-analyze', 'id' => $isp->id]), 'btn_class' => 'btn btn-xs btn-default', 'tip' => '分析'],
                    ]]);?></td>
            </tr>
        <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script>
    /**
     * 删除运营商
     * @param id int 运营商编号
     */
    function deleteIsp(id) {
        if (!confirm('确定要删除此运营商吗？\n请先确保此运营商下所有的卡都已回收。')) {
            return false;
        }
        $.getJSON('<?php echo Url::to(['/admin/card/delete-isp']);?>', {id: id}, function (json) {
            if (callback(json)) {
                $('#data_' + id).remove();
            }
        });
    }
</script>
